package com.moweilin.mo_gateway.spi.impl;

import com.moweilin.mo_gateway.servers.MoGatewayServer;
import com.moweilin.mo_gateway.spi.IConfigProvider;
import io.vertx.core.Verticle;

import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * Created by moweilin on 2017/2/28.
 */
public class DefaultConfigProvider implements IConfigProvider {
    private static DefaultConfigProvider instance;

    private String resource;
    private Class<? extends Verticle> serverClass;
    private void init(){
        ServiceLoader<IConfigProvider> loader = ServiceLoader.load(IConfigProvider.class);
        Iterator<IConfigProvider> iterator = loader.iterator();
        while (iterator.hasNext()){
            IConfigProvider provider = iterator.next();
            if(resource == null){
                resource = provider.getResource();
            }
            if(serverClass == null){
                serverClass = provider.getServerClass();
            }
        }
        if(resource == null){
            resource = "com.moweilin.mo_gateway.MoGatewayResource";
        }
        if(serverClass == null){
            serverClass = MoGatewayServer.class;
        }
    }

    public static IConfigProvider getInstance() {
        if(instance == null){
            instance = new DefaultConfigProvider();
            instance.init();
        }
        return instance;
    }

    @Override
    public String getResource() {
        return resource;
    }

    @Override
    public Class<? extends Verticle> getServerClass() {
        return serverClass;
    }

}
